<?php
    class access_token{
        const CACHE_HANDLE="class.weixin.access_token";
        
        function get_token(){
            if($token=$this->get_token_cache()){
                return $token;
            }
            else{
                return $this->get_access_token();
            }
        }
        
        function get_js_token(){
            if($token=$this->get_js_token_cache()){
                return $token;
            }
            else{
                return $this->get_jsticket_token();
            }
        }
        
        function get_card_token(){
            if($token=$this->get_card_token_cache()){
                return $token;
            }
            else{
                return $this->get_card_api_ticket();
            }
        }
        
        function get_token_cache(){
            include_once BASE_PATH.'/include/cache.inc.php';
            $cache=cache::get_cache_handle("file");
            if($data=$cache->get_data($this::CACHE_HANDLE)){
                if(time()<($data['get_time']+$data['expires_in'])){
                    return $data['access_token'];
                }
            }
        }
        
        function set_token_cache($data){
            include_once BASE_PATH.'/include/cache.inc.php';
            $cache=cache::get_cache_handle("file");
            $cache->set_data($this::CACHE_HANDLE,$data);
        }
        
        function get_js_token_cache(){
            include_once BASE_PATH.'/include/cache.inc.php';
            $cache=cache::get_cache_handle("file");
            if($data=$cache->get_data("class.weixin.js_token")){
                if(time()<($data['get_time']+$data['expires_in'])){
                    return $data['ticket'];
                }
            }
        
        }
        
        function set_js_token_cache($data){
            include_once BASE_PATH.'/include/cache.inc.php';
            $cache=cache::get_cache_handle("file");
            $cache->set_data("class.weixin.js_token",$data);
        }
        
        function get_card_token_cache(){
            include_once BASE_PATH.'/include/cache.inc.php';
            $cache=cache::get_cache_handle("file");
            if($data=$cache->get_data("class.weixin.card_token")){
                if(time()<($data['get_time']+$data['expires_in'])){
                    return $data['ticket'];
                }
            }
        }
        
        
        function set_card_token_cache($data){
            include_once BASE_PATH.'/include/cache.inc.php';
            $cache=cache::get_cache_handle("file");
            $cache->set_data("class.weixin.card_token",$data);
        }
        
        function create_get_token_url(){
            $appid = PUSH_MESSAGE_TO_APP_APPID;
            $AppSecret = APPSECRET;
            return "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$AppSecret";
        }
        
        function get_access_token(){
            $ch = curl_init($this->create_get_token_url($this->create_get_token_url())); 
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 0);
            $data = curl_exec($ch);
            $data=json_decode($data,true);
            if(is_array($data)){
                $data['get_time']=time();
            }
            curl_close($ch);
            if(isset($data['access_token'])){
                $this->set_token_cache($data);
                return $data['access_token'];
            }
            else{
//                 throw new my_exception("‘ACCESS_TOKEN’获取失败",SYS_ERROR,1);
            }
        }
        
        function get_card_api_ticket(){
            $access_token=$this->get_access_token();
            $ch = curl_init("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$access_token&type=wx_card");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 0);
            $data = curl_exec($ch);
            $data=json_decode($data,true);
            if(is_array($data)){
                $data['get_time']=time();
            }
            curl_close($ch);
            if(isset($data['ticket'])){
                $this->set_card_token_cache($data);
                return $data['ticket'];
            }
            else{
                //                 throw new my_exception("‘JS_TICKET’获取失败",SYS_ERROR,1);
            }
        }
        
        function get_jsticket_token(){
            $access_token=$this->get_access_token();
            $ch = curl_init("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$access_token&type=jsapi");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 0);
            $data = curl_exec($ch);
            $data=json_decode($data,true);
            if(is_array($data)){
                $data['get_time']=time();
            }
            curl_close($ch);
            if(isset($data['ticket'])){
                $this->set_js_token_cache($data);
                return $data['ticket'];
            }
            else{
//                 throw new my_exception("‘JS_TICKET’获取失败",SYS_ERROR,1);
            }
        }
        
    }